package com.example.project10;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import java.util.List;

public class UserViewModel extends AndroidViewModel {
    private static final String TAG = "UserViewModel";
    
    private MutableLiveData<List<User>> userListLiveData;
    private MutableLiveData<Boolean> isLoadingLiveData;
    private MutableLiveData<String> errorLiveData;
    
    private UserRepository userRepository;
    
    public UserViewModel(@NonNull Application application) {
        super(application);
        userListLiveData = new MutableLiveData<>();
        isLoadingLiveData = new MutableLiveData<>();
        errorLiveData = new MutableLiveData<>();
        
        userRepository = new UserRepository(application.getApplicationContext());
    }
    
    public LiveData<List<User>> getUserList() {
        return userListLiveData;
    }
    
    public LiveData<Boolean> getIsLoading() {
        return isLoadingLiveData;
    }
    
    public LiveData<String> getError() {
        return errorLiveData;
    }
    
    /**
     * 加载用户列表
     */
    public void loadUsers() {
        isLoadingLiveData.setValue(true);
        userRepository.fetchUsers(userListLiveData, errorLiveData, isLoadingLiveData);
    }
    
    /**
     * 刷新用户列表
     */
    public void refreshUsers() {
        loadUsers();
    }
}